The difference between char STR [] and char * STR is resolved in detail

  • 2020-04-02 01:36:40
  • OfStack


char* get_str(void)  
{  
    char str[] = {"abcd"};  
    return str;  
}

Char STR [] = {" abcd "}; Defines an array of local characters. Although it is an array, it is a local variable, and the address that returns it must be the address of a freed space.

This function returns the address of an internal local array of characters, STR, which is destroyed after the function is called, so you return a pointer to a destroyed block of memory.


char* get_str(void)  
{  
    char *str = {"abcd"};  
    return str;  
}  

Char * STR = {" abcd "}; Defines a string constant and assigns its address to STR.
This function returns the address string constants, and like this kind of string are global, at compile time has been allocated memory, only the end of the program will be destroyed, so return it address is no problem, but you'd better return a pointer to a constant, because you can't change the value of the string constants.

const char str[] = "abcd";        //The ABC is stored on the stack & NBSP;
const char *str = "abcd";         //ABC is stored in the static storage area & NBSP;

To be exact, the above two "abcs" are stored in the static storage area, that is, the constant area. Constant areas are readable and unwritable. So any attempt to write to the constant area of the operation is illegal, of course, this is not necessarily impossible to write, you can take some kind of channel to change the constant area of the memory property, such as PE related section of the property can be read and write to the constant area, of course, this can be ignored at present...

So why STR [] = "ABC ";          
Can I write it?    
The answer is STR [] = "ABC"; There is an extra copy process where the "ABC" of the constant block is copied into the stack so that it can be written.

Conclusion:
All characters and strings containing "" or" "are constants and should be stored on the heap.


char *str = "xxxxx" . str Points to the constant address. 
char str[] = "xxxxx" . str Request space on the stack, copy in the constant content, so it is a local variable. 

First of all, arrays and Pointers are different data types, with essential differences:

char str[] = "abcd";  //sizeof(str) == 5 * sizeof(char)
char * str = "abcd";  //sizeof(str) == 4(x86) or 8(x64)

Arrays can be converted to Pointers automatically. Pointers cannot be converted to arrays.

Then, a string is equivalent to an array of characters, not to a character pointer. According to the above, the string can be automatically converted to a character pointer.

Then, "abcd" is called "string constant." any type of constant is an rvalue (a temporary variable without a name), and you must make "abcd" an lvalue (a variable with a name) to modify the string "abcd."


char str[] = "abcd"; //Both sides of the equality are the same data type, and the right value becomes the left value
char * str = "abcd"; //There are different data types on both sides of the equality, and the right end is automatically converted to char*, which gets the name of STR, while the char array "abcd" still has no name.

Char * STR is stored in the global static store, so the function returns with the correct value even though it is a local variable!
Char STR [] is stored on the stack, local variable. After the function returns, the OS takes back the space and no longer exists, so it can't get the correct result!

Char STR [] = "name"; And char STR [5]; What's the difference between STR ="name"? Can you tell me from the memory allocation point of view, I know that the array name is a constant address (pointer), why is the first one right and why is the second one wrong?

The second one first defines an array. Remember that the array name STR is the first address of the space to which the array is allocated. STR ="name" should be an error of type mismatch on both sides. General constants should have no memory address unless a variable points to the constant.

Array names are address constants, and of course constants are not allowed to be reassigned.
"Name" is a string constant that is stored in the constant store and can only be pointed to with one pointer but cannot be changed: char*p; P = "name";
Char STR []="name"; Arrays are allocated on the stack by the compiler, and the contents can be changed by the user.


Related articles: